package dissertation.extensions.beans.base.view;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.ui.DocumentAdapter;

import javax.swing.*;
import javax.swing.event.DocumentEvent;

public class CreateEJBDialogWrapper extends DialogWrapper {

    private InputValidator myFieldValidator;
    private CreateEJBPanel createEJBPanel;
    private String ejbNameSuffix;
    private String ejbClassNameSuffix;

    public CreateEJBDialogWrapper(final Project project, InputValidator inputValidator,
                                  String title, String ejbNameSuffix, String ejbClassNameSuffix) {
        super(project, true);
        this.myFieldValidator = inputValidator;
        this.ejbClassNameSuffix = ejbClassNameSuffix;
        this.ejbNameSuffix = ejbNameSuffix;
        init();
        setResizable(false);
        setTitle(title);
        setOKActionEnabled(false);
    }

    @Override
    protected JComponent createCenterPanel() {
        DocumentAdapter documentAdapter = new DocumentAdapter() {
            protected void textChanged(DocumentEvent e) {
                setOKActionEnabled(!createEJBPanel.getjTextFieldEJBName().getText().isEmpty());
                createEJBPanel.getjTextFieldEJBClass().setText(createEJBPanel.getjTextFieldEJBName().getText());
            }
        };

        createEJBPanel = new CreateEJBPanel(documentAdapter, ejbNameSuffix, ejbClassNameSuffix);
        return createEJBPanel;
    }

    protected void doOKAction() {
        String inputString = getEJBClassName();
        if (myFieldValidator.checkInput(inputString) && myFieldValidator.canClose(inputString)) {
            close(OK_EXIT_CODE);
        }
        close(OK_EXIT_CODE);
    }

    public String getEJBName() {
        return createEJBPanel.getjTextFieldEJBName().getText();
    }

    public String getEJBClassName() {
        return createEJBPanel.getjTextFieldEJBClass().getText();
    }
}
